package cn.piesat.scanning.business.xz.service.gseimpl;

import cn.hutool.core.util.StrUtil;
import cn.piesat.scanning.business.xz.service.BuildAlgorithmTaskService;
import cn.piesat.scanning.business.xz.single.AlgorithmTaskPathParam;
import cn.piesat.scanning.business.xz.single.AlgorithmTaskRuleParam;
import cn.piesat.scanning.dto.HtScanningMetadataDTO;
import cn.piesat.scanning.service.HtScanningMetadataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Ming
 * @version 1.0
 * @description: TODO
 * @date 2023/12/18 13:48
 */
@Service
public class BuildTibet_Prod_COMBServiceImpl implements BuildAlgorithmTaskService {

    @Autowired
    private HtScanningMetadataService htScanningMetadataService;
    @Override
    public Map<String, Object> initAlgorithmParams(String adsType, String primaryFile, String filePath, AlgorithmTaskRuleParam taskRuleParam, AlgorithmTaskPathParam taskPathParam, Map<String, Integer> indexParams, String fileDate, String scanningPath,HtScanningMetadataDTO htScanningMetadataDTO) {
        Map<String, Object> algorithmParams = new HashMap<>();
        //草地_生物量_季年合成
        //GSE_FAGB_540000_L3_FY3D_MERSI_1000M_GLL_MOAM_20211101000000_20211130000000.TIFF
        String fileName = primaryFile.substring(primaryFile.lastIndexOf("/") + 1);
        String[] s1 = fileName.split("_");
        String moduleIid = s1[0];
        String prodcut = s1[1];
        String region = s1[2];
        String level = s1[3];
        String satellite = s1[4];
        String productMode = s1[8];
        List<HtScanningMetadataDTO> metadataDTOList = new ArrayList<>();
        if (taskPathParam.getParameter04().contains("AQ")) {
            String month = s1[9].substring(4, 6); // 假设月份信息在日期字符串的第5位和第6位
            String quarter = null;
            switch (month) {
                case "03":
                case "04":
                case "05":
                    quarter = "1";
                    break;
                case "06":
                case "07":
                case "08":
                    quarter = "2";
                    break;
                case "09":
                case "10":
                case "11":
                    quarter = "3";
                    break;
                case "12":
                case "01":
                case "02":
                    quarter = "4";
                    break;
            }
            String year = s1[9].substring(0, 4);
            String startTime = null, endTime = null;
            if ("1".equals(quarter)) {
                startTime = year + "0301000000";
                endTime = year + "0531235959";
            } else if ("2".equals(quarter)) {
                startTime = year + "0601000000";
                endTime = year + "0831235959";
            } else if ("3".equals(quarter)) {
                startTime = year + "0901000000";
                endTime = year + "1131235959";
            } else if ("4".equals(quarter)) {
                startTime = year + "12000000";
                int i = Integer.parseInt(year);
                int nextYear = i + 1;
                endTime = nextYear + "0231235959";
            }
            metadataDTOList = htScanningMetadataService.findOneByDate(startTime, endTime, level, satellite,
                    prodcut.replace("-", ""), region, productMode, moduleIid, "TIFF");
            List<String> collect = metadataDTOList.stream().map(x -> taskPathParam.getParameter02() + File.separator + x.getFilePath()).collect(Collectors.toList());
            if (metadataDTOList.isEmpty()) {
                algorithmParams.put("parameter01", null);
            } else {
                Map<String, Object> params = new HashMap<>();
                String join = StrUtil.join(",", collect);
                params.put("primaryFile", join);
                params.put("CType", taskPathParam.getParameter04());
                params.put("auxPath", taskPathParam.getParameter03());
                algorithmParams.put("parameter00", adsType);
                algorithmParams.put("parameter01", params);
            }
        } else if (taskPathParam.getParameter04().contains("AY")) {
            //BAS_NDVI_540000_L3_FY3D_MERSI_1000M_GLL_MOAD_20230701000000_20230701235959.TIFF
            String date = s1[9].substring(0, 4);
            String startTime = date + "0101000000";
            String endTime = date + "1231235959";
            metadataDTOList = htScanningMetadataService.findOneByDate(startTime, endTime, level, satellite,
                    prodcut.replace("-", ""), region, productMode, moduleIid, "TIFF");
            List<String> collect = metadataDTOList.stream().map(x -> taskPathParam.getParameter02() + File.separator + x.getFilePath()).collect(Collectors.toList());
            if (metadataDTOList.isEmpty()) {
                algorithmParams.put("parameter01", null);
            } else {
                Map<String, Object> params = new HashMap<>();
                String join = StrUtil.join(",", collect);
                params.put("primaryFile", join);
                params.put("CType", taskPathParam.getParameter04());
                params.put("auxPath", taskPathParam.getParameter03());
                algorithmParams.put("parameter00", adsType);
                algorithmParams.put("parameter01", params);
            }
        }
        return algorithmParams;
    }
}
